package com.huan;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;


//客户端开发
public class Test7_telnet_baidu {
    public static void main(String[] args) {
        String website = "www.baidu.com";
        int port = 80;
        //应用层协议
        String http = "GET / HTTP/1.0\r\n\r\n";
        //http服务器-》http服务器
        try(
                Socket socket = new Socket(website,port);
                OutputStream outputStream = socket.getOutputStream();
                InputStream inputStream = socket.getInputStream();
                ){
            outputStream.write(http.getBytes());
            outputStream.flush();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bytes = new byte[10*1024];
            int length = -1;
            while ((length = inputStream.read(bytes,0,bytes.length))!=-1){
                byteArrayOutputStream.write(bytes,0,length);
            }
            byteArrayOutputStream.flush();
            byte[] b = byteArrayOutputStream.toByteArray();
            String s = new String(b);
            System.out.println(s);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
